#include "EventAddWindow.h"

#include "EventClient.h"
#include "object_v2/object_v2-string.h"

/*若有空条目时点击确认，对应的空条目会变成红色，此变量置 true*/
static gboolean entry_wait_recovery = false;

struct _EventAddWindow
{
        AdwWindow parent_instance;

        // GtkHeaderBar *header_bar;
        GtkButton    *cancel_button;
        GtkButton    *ok_button;
        AdwEntryRow  *end_time_set;
        AdwEntryRow  *event_name;
        AdwEntryRow  *event_content;
        GtkSwitch    *is_long_event_button;

        ObjectString *date_str;
        ObjectString *name_str;
        ObjectString *content_str;

        gboolean      has_info;
};

G_DEFINE_FINAL_TYPE(EventAddWindow, event_add_window, ADW_TYPE_WINDOW)

static void
event_add_window_finalize(GObject *object)
{
        EventAddWindow *self = EVENT_ADD_WINDOW(object);

        object_unref(self->date_str);
        object_unref(self->name_str);
        object_unref(self->content_str);

        G_OBJECT_CLASS(event_add_window_parent_class)->finalize(object);
}

static void
cancel_action(EventAddWindow *self)
{
        gtk_window_close(GTK_WINDOW(self));
}

static gboolean
verify_has_content(const char *content)
{
        if (!content)
                return false;
        return *content != '\0';
}

static void
high_light_with_content(AdwEntryRow *entry)
{
        GtkWidget  *frame = gtk_widget_get_parent(GTK_WIDGET(entry));
        const char *css[] = {"item-color-red", NULL};
        gtk_widget_set_css_classes(frame, css);
}

static gboolean
verify_date_is_correct(const char *date)
{
        int i;
        for (i = 1; *(++date) != '\0' && i <= 9; ++i)
                if (!(*date >= '0' && *date <= '9'))
                        break;
        return i == 8;
}

static void
apply_action(EventAddWindow *self)
{
        const char *end_time_set, *event_name, *event_content;

        end_time_set = gtk_editable_get_text(GTK_EDITABLE(self->end_time_set));
        event_name   = gtk_editable_get_text(GTK_EDITABLE(self->event_name));
        event_content =
                gtk_editable_get_text(GTK_EDITABLE(self->event_content));

        if (!*end_time_set || !*event_name || !*event_content)
                entry_wait_recovery = true;

        if (!verify_has_content(end_time_set) ||
            !verify_date_is_correct(end_time_set)) {
                high_light_with_content(self->end_time_set);
                return;
        }

        if (!verify_has_content(event_name)) {
                high_light_with_content(self->event_name);
                return;
        }

        if (!verify_has_content(event_content)) {
                high_light_with_content(self->event_content);
                return;
        }

        object_string_set_string(self->date_str, end_time_set);
        object_string_set_string(self->name_str, event_name);
        object_string_set_string(self->content_str, event_content);

        self->has_info = true;
        gtk_window_close(GTK_WINDOW(self));
}

static void
entring(AdwEntryRow *entry)
{
        GtkWidget  *frame;
        const char *css[] = {NULL, NULL};
        if (entry_wait_recovery) {
                frame = gtk_widget_get_parent(GTK_WIDGET(entry));
                gtk_widget_set_css_classes(frame, css);
                entry_wait_recovery = false;
        }
}

const char *
event_add_window_get_event_date(EventAddWindow *self)
{
        return object_string_get_string(self->date_str);
}

const char *
event_add_window_get_event_name(EventAddWindow *self)
{
        return object_string_get_string(self->name_str);
}

const char *
event_add_window_get_event_content(EventAddWindow *self)
{
        return object_string_get_string(self->content_str);
}

gboolean
event_add_window_can_get_info(EventAddWindow *self)
{
        return self->has_info;
}

static void
event_add_window_class_init(EventAddWindowClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS(klass);
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

        object_class->finalize       = event_add_window_finalize;

        gtk_widget_class_set_template_from_resource(
                widget_class,
                "/org/korack/event/EventAddWindow.ui");

        gtk_widget_class_bind_template_child(widget_class,
                                             EventAddWindow,
                                             cancel_button);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventAddWindow,
                                             ok_button);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventAddWindow,
                                             event_name);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventAddWindow,
                                             event_content);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventAddWindow,
                                             is_long_event_button);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventAddWindow,
                                             end_time_set);

        gtk_widget_class_bind_template_callback(widget_class, cancel_action);
        gtk_widget_class_bind_template_callback(widget_class, apply_action);
        gtk_widget_class_bind_template_callback(widget_class, entring);
}

EventAddWindow *
event_add_window_new()
{
        return g_object_new(EVENT_TYPE_ADD_WINDOW, NULL);
}

static void
event_add_window_init(EventAddWindow *self)
{
        gtk_widget_init_template(GTK_WIDGET(self));

        self->date_str    = object_string_new();
        self->name_str    = object_string_new();
        self->content_str = object_string_new();
        self->has_info    = false;
}